using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Betarium.PotonEditor
{
    public class ToolInfoText : ToolInfoSelect
	{
        protected volatile string m_sText;
        protected Font m_TargetFont;
        private volatile Image m_PasteImage;

        public Image PasteImage
        {
            get { return m_PasteImage; }
            set { m_PasteImage = value; }
        }

		public ToolInfoText(EditViewForm form)
			: base(form)
		{
            m_TargetFont = m_Form.PaintState.FontInfo;
		}

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 2 && m_PasteImage != null)
            {
                Commit();
                return;
            }
            if (e.Button == MouseButtons.Left && e.Clicks == 2 && m_PasteImage == null)
            {
                Edit();
                return;
            }
            base.OnMouseDown(sender, e);
            if(!m_Form.SelectState.Selected && m_PasteImage != null)
            {
                Reset();
            }
        }
        public void Commit()
        {
            if (m_PasteImage != null)
            {
                this.m_Form.AddUndo();
                using (Graphics graph = Graphics.FromImage(m_Form.Image))
                {
//                    graph.DrawImage(m_PasteImage, m_Form.SelectState.SelectRect, new Rectangle(Point.Empty, m_PasteImage.Size), GraphicsUnit.Pixel);
                    graph.DrawImage(m_PasteImage, m_Form.SelectState.SelectRect, new Rectangle(Point.Empty, m_Form.SelectState.SelectRect.Size), GraphicsUnit.Pixel);
                    graph.Dispose();
                }
                m_Form.SetModified(true);
            }
            Reset();
        }
        public void Reset()
        {
            if (m_PasteImage != null)
            {
                m_PasteImage.Dispose();
                m_PasteImage = null;
            }
//            m_Form.SetPainter(EditViewForm.ToolType.Select);
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
		{
            if (m_SelectModeType == SelectModeTypes.Selecting)
            {
                Edit();
            }
            base.OnMouseUp(sender, e);
		}

        public override void OnPaint(object sender, PaintEventArgs e)
        {
            if (m_PasteImage != null && m_Form.Selected)
            {
                e.Graphics.DrawImage(m_PasteImage, m_Form.SelectState.SelectRect.Left, m_Form.SelectState.SelectRect.Top, new Rectangle(new Point(), m_PasteImage.Size), GraphicsUnit.Pixel);
                Color color = Color.FromArgb(128, Color.Gray);
                e.Graphics.FillRectangle(new SolidBrush(color), m_Form.SelectState.SelectRect);
            }
        }

        public override void Dispose()
        {
            if(m_TargetFont != null)
            {
                m_TargetFont.Dispose();
            }
            if(m_PasteImage != null)
            {
                m_PasteImage.Dispose();
            }
            base.Dispose();
        }

        public void Edit(object sender, EventArgs e)
        {
            Edit();
        }
        public void RefreshImage()
        {
            Size size = this.m_Form.SelectState.SelectRect.Size;
            Point pos = this.m_Form.SelectState.SelectRect.Location;
            Bitmap image = new Bitmap(size.Width, size.Height);
            using (Graphics graph = Graphics.FromImage(image))
            {
                graph.DrawString(m_sText, m_TargetFont, new SolidBrush(m_Form.PaintState.MainColor), new PointF());
            }
            if (m_PasteImage != null)
            {
                m_PasteImage.Dispose();
            }
            m_PasteImage = image;
        }
        public void Edit()
        {
            if (m_Form.SelectState.SelectRect.Size.IsEmpty)
            {
                return;
            }
            TextInputForm form = new TextInputForm();
            form.TargetFont = m_TargetFont;
            form.textBox.Text = m_sText;
            if (form.ShowDialog() == DialogResult.OK)
            {
                m_Form.PaintState.FontInfo = form.TargetFont;
                m_TargetFont = form.TargetFont;
                m_sText = form.textBox.Text;
                System.Diagnostics.Debug.WriteLine("draw text:" + m_sText);
                RefreshImage();
                m_Form.Refresh();
            }
        }
        public override void OnPaintStateChanged()
        {
            RefreshImage();
            m_Form.Refresh();
        }
    }
}
